﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO.IsolatedStorage;

namespace gears
{
    class LevelManager
    {
        private Player player;
        List<Level> levels = new List<Level>();
        private int active_level = 0;
        private SpriteFont text_font;
        private bool game_end = false;
        private Vector2 credits_position = new Vector2(400,0);
        private Texture2D credit_background;
        private Texture2D credits_box;
        private SpriteBatch spriteBach; // itt <========================== 
        private Tutorial tutorial;
        List<GearWheel> gear_wheel = new List<GearWheel>();


        public LevelManager(SpriteBatch sb)
        {
            player = new Player();

            levels.Add(new Level("background",sb,player));

            text_font = ContentLoader.LoadFont("Sound/myFont");
            credit_background = ContentLoader.LoadTexture("credit_background");
            credits_box = ContentLoader.LoadTexture("credits_box");

            this.spriteBach = sb;  //<<========================================================= 

            tutorial = new Tutorial();

            gear_wheel.Add(new GearWheel(new Vector2(148, 110), 15, -0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            gear_wheel.Add(new GearWheel(new Vector2(148, 265), 15, -0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            gear_wheel.Add(new GearWheel(new Vector2(148, 410), 15, -0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            gear_wheel.Add(new GearWheel(new Vector2(652, 120), 15, 0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            gear_wheel.Add(new GearWheel(new Vector2(652, 265), 15, 0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            gear_wheel.Add(new GearWheel(new Vector2(652, 400), 15, 0.0165f, ContentLoader.LoadTexture("wireWheel"), ContentLoader.LoadTexture("wireTooth"), ContentLoader.LoadTexture("smallNumbers/num1")));
            gear_wheel[gear_wheel.Count - 1].IsFixed = true;
            gear_wheel[gear_wheel.Count - 1].Drived = true;
            
        }

        public void Update(GameTime game_time)
        {
            if (game_end == false)
            {
                CheckLevelComplete();
                
                if (Player.PlayerActivity == true)
                {
                    CheckCollisions();
                    player.Update(game_time);
                }
                levels[active_level].Update(game_time);

                levels[active_level].CheckStageCorrection(player);

               // levels[active_level].CheckCollisionTerminalWithPlayer(player);

                if (Player.PlayerLive == false)
                {
                    //levels[active_level] = new Level(levels[active_level].LevelName, spriteBach,player); //<<=========================================
                    if (Level.current_mech == 0)
                    {
                        levels[active_level].InitMech1(levels[active_level].sb);
                        player.CollisionWithMechanism = false;
                        Player.PlayerLive = true;
                        Player.PlayerActivity = true;
                        player.PlayerPosition = new Vector2(700, 50);
                    }
                    else if(Level.current_mech==1)
                    {
                        levels[active_level].InitMech2(levels[active_level].sb);
                        player.CollisionWithMechanism = false;
                        Player.PlayerLive = true;
                        Player.PlayerActivity = true;
                        player.PlayerPosition = new Vector2(700, levels[active_level].LevelMechanisms[1].Position.Y - 50);
                        levels[active_level].levelState = 4;

                        levels[active_level].level_platforms_container[3].ItemPosition = new Vector2(levels[active_level].level_platforms_container[3].ItemPosition.X,350);
                        levels[active_level].level_platforms_container[4].ItemPosition = new Vector2(levels[active_level].level_platforms_container[4].ItemPosition.X, 350);
                    }
                    Level.current_mech = -1;

                 

                }
                tutorial.UpdateTutorial();
            }
            else
            {
                UpdateCredits();
            }
        }

        private void CheckCollisions()
        {
            Rectangle playerRect = new Rectangle((int)player.PlayerPosition.X,
                                                (int)player.PlayerPosition.Y,
                                                 player.PlayerAnimation.SpriteWidth+(int)player.moveSpeed.X, 
                                                 player.PlayerAnimation.SpriteHeight+(int)player.moveSpeed.Y);
            //  check collisions with platforms
            foreach (LevelPlatformsContainer platform in levels[active_level].LevelPlatforms)
            {
                Rectangle platformRect = new Rectangle((int)platform.ItemPosition.X, (int)platform.ItemPosition.Y,
                                                        platform.ItemTexture.Width, platform.ItemTexture.Height);
                if (playerRect.Intersects(platformRect))
                {
                    //  tell the player he is collide wit one platform(pos, dimensions(width, height))
                    player.CollideWithPlatform(platform.ItemPosition, new Vector2(platform.ItemTexture.Width, platform.ItemTexture.Height),platform.Trap);
                }
            }

           foreach (Mechanism mech in levels[active_level].LevelMechanisms)
            {
                Rectangle mechanismRect = new Rectangle((int)mech.Position.X, (int)mech.Position.Y, (int)mech.Dimensions.X, (int)mech.Dimensions.Y);
                if (playerRect.Intersects(mechanismRect))
                {
                    player.CollideWithMechanism(mech);
                    mech.CollideWithPlayer();
                }
            }

            foreach (Stage stage in levels[active_level].LevelStages)
            {

                    Mechanism mech = stage.Mechanism;

                    Rectangle mechanismRect = new Rectangle((int)mech.Position.X, (int)mech.Position.Y, (int)mech.Dimensions.X, (int)mech.Dimensions.Y);
                    if (playerRect.Intersects(mechanismRect))
                    {
                        player.CollideWithMechanism(mech);
                        mech.CollideWithPlayer();
                    }

                    Rectangle stage_door = new Rectangle((int)(stage.StageDoorPosition.X), (int)stage.StageDoorPosition.Y, stage.DoorTextureCurrentWidth, stage.DoorTextureCurrentHeight);

                    if (playerRect.Intersects(stage_door))
                    {
                        if (stage.StageDoorTexture.Height == stage.DoorTextureCurrentHeight)
                        {
                            player.Direction = "right";
                            player.CollideWithPlatform(new Vector2(stage.StageDoorPosition.X, stage.StageDoorPosition.Y +5), new Vector2(stage.DoorTextureCurrentWidth, stage.DoorTextureCurrentHeight), false);
                        }
                        else if (stage.DoorTextureCurrentHeight>50 && stage.DoorTextureCurrentHeight<stage.StageDoorTexture.Height)
                        {
                            player.CollideWithPlatform(new Vector2(stage.StageDoorPosition.X, stage.StageDoorPosition.Y + 5), new Vector2(stage.StageDoorTexture.Width, stage.StageDoorTexture.Height), false);
                        }
                        else
                        {

                            player.CollideWithPlatform(new Vector2(stage.StageDoorPosition.X, stage.StageDoorPosition.Y), new Vector2(stage.DoorTextureCurrentWidth, stage.DoorTextureCurrentHeight), false);
                        }
                    }

                    if (stage.KillerType == "box")
                    {
                        Rectangle stage_killer = new Rectangle((int)stage.KillerPosition[0].X,(int)stage.KillerPosition[0].Y,stage.KillerTexture.Width,stage.KillerTexture.Height);

                        if (playerRect.Intersects(stage_killer))
                        {
                            player.Direction = "left";
                            player.CollideWithPlatform(new Vector2(stage.KillerPosition[0].X, stage.KillerPosition[0].Y + 5), new Vector2(stage.KillerTexture.Width, stage.KillerTexture.Height), false);
                        }
                    }
                    if (stage.KillerType == "sand")
                    {
                        Rectangle tube_door = new Rectangle(0,365,30,100);

                        if(playerRect.Intersects(tube_door))
                        {
                            player.CollideWithPlatform(new Vector2(0, 365), new Vector2(30, 100), false);
                        }
                    }
                
            }

        }

        private void CheckLevelComplete()
        {
            if (levels[active_level].LevelComplete == true && levels[active_level].CheckPlayerIsGateway(player)==true)
            {
                if (active_level < levels.Count - 1)
                {
                    active_level++;
                    player.PlayerPosition = new Vector2(0, 100);
                    player.PlayerInventory.Clear();
                }
                else
                {
                    game_end = true;
                }

            }
        }

        public void LevelManagerDraw(SpriteBatch sprite_batch)
        {
            if (game_end == true)
            {
                DrawCredits(sprite_batch);
            }
            else if (levels.Count > 0 && game_end == false)
            {
                
                levels[active_level].DrawLevel(sprite_batch);
                player.DrawPlayer(sprite_batch);

                foreach (Stage _stage in levels[active_level].LevelStages)
                {
                    _stage.DrawStageKillers(sprite_batch);
                }

                tutorial.DrawTutorial(sprite_batch);
               
            }
            
        }

        private void UpdateCredits()
        {
            credits_position = new Vector2(credits_position.X, credits_position.Y + 0.8325f);
            if (credits_position.Y > 1420)
            {
                Game1.backButtonPressed = true;

                credits_position = new Vector2(credits_position.X, 0);
                
            }
            foreach (GearWheel gw in gear_wheel)
            {
                gw.Rotate();
            }
        }

        private void DrawCredits(SpriteBatch sprite_batch)
        {
            sprite_batch.Draw(credit_background, new Vector2(-10,-25), Color.White);

            sprite_batch.DrawString(text_font, "Mentor: Dr.Zlatko Covic", new Vector2(230, 560-credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 525 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "Dima Daniel", new Vector2(320, 710 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 675 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "Fodor Balazs", new Vector2(320, 860 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 825 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "Kovacs Bence", new Vector2(310, 1010 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 975 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "Maronka Jozsef", new Vector2(300, 1160 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 1125 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "VTS Subotica 2014", new Vector2(270, 1310 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 1275 - credits_position.Y), Color.White);
            sprite_batch.DrawString(text_font, "Team: Senseiz", new Vector2(300, 1460 - credits_position.Y), Color.Black);
            sprite_batch.Draw(credits_box, new Vector2(210, 1425 - credits_position.Y), Color.White);

            foreach (GearWheel gw in gear_wheel)
            {
                gw.Draw(sprite_batch);
            }
        }

        public bool GameEnded
        {
            get
            {
                return game_end;
            }
        }
    }
}
